<template>

  <div>

    <!--二维码/标签打印页面-->
    <div   v-show="printFlag"
           style="position: absolute;top:100px;width: 700px;height: 370px;z-index: 700;background-color: white;border: 2px silver solid;left: 30%">
      <!--      {{ printForm }}-->
      <div  id="print-content" ref="print" >
        <table id="printArea" v-for="(item,index) in this.checkedMDoboxInfoDetail" :key="index"  border="1px solid \#ccc" cellspacing="0" cellpadding="0" style="text-align: center">
          <tr>
            <td style="height: 50px;width: 100px">
              任务令
            </td>
            <td colspan="3" style="width: 200px">
              {{item.taskTon}}
            </td>
          </tr>
<!--          <tr>-->
<!--            <td style="height: 70px;">-->
<!--              工单数量-->
<!--            </td>-->
<!--            <td style="width: 370px">-->
<!--              {{item.orderNumber}}-->
<!--            </td>-->
<!--          </tr>-->
          <tr>
            <td style="height: 50px;width: 100px">
              物料名称
            </td>
            <td colspan="3" style="width: 200px">
              {{item.materialName}}
            </td>
          </tr>

          <tr>
            <td style="height: 50px;width: 100px">
              物料编码
            </td>
            <td style="width: 200px">
              {{item.materielid}}
            </td>
            <td style="height: 50px;width: 100px">
              工位编码
            </td>
            <td style="width: 200px">
              {{item.routingCode}}
            </td>
          </tr>

          <tr>
            <td style="height: 50px;width: 100px">
              产线编码
            </td>
            <td style="width: 200px">
              {{item.lineCode}}
            </td>
            <td style="height: 50px;width: 100px">
              物料数量
            </td>
            <td style="width: 200px">
              {{item.kitquantity}}
            </td>
          </tr>
          <!-- 添加一个不可见的标签用于创建空格 -->
          <div style="height: 20px; visibility: hidden;"></div>
        </table>
      </div>
    </div>

    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="80px">
      <el-form-item label="工单ID" prop="orderId" v-if="false" label-width="70px">
        <el-input
          v-model="queryParams.orderId"
          :disabled="true"
        />
      </el-form-item>
      <el-form-item label="物料编码" prop="materielid">
        <el-input v-model="queryParams.materielid" />
      </el-form-item>
      <el-form-item label="任务令" prop="materialName">
        <el-input v-model="queryParams.taskTon" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          class="print-button"
          type="danger"
          size="mini"
          :disabled="multiple"
          @click="handleBatchPrintCode"
        >
          <i class="el-icon-printer">打印</i>
        </el-button>
      </el-col>
    </el-row>

    <el-table
      :show-selection="true"
      :data="dataList"
      @selection-change="handleSelectionChange"
      border>
      <el-table-column
        type="selection"
        width="60"
        align="center"
        fixed="left"
      />
      <el-table-column label="工单id" align="center" prop="orderId" v-if="false"/>
      <el-table-column label="工单编码" align="center" prop="orderCode" />
      <el-table-column label="工单数量" align="center" prop="orderNumber" />
      <el-table-column label="物料编码" align="center" prop="materielid" />
      <el-table-column label="物料名称" align="center" prop="materialName" />
      <el-table-column label="物料数量" align="center" prop="kitquantity" />
      <el-table-column label="任务令" align="center" prop="taskTon" />
      <el-table-column label="工位编码" align="center" prop="routingCode" />
      <el-table-column label="产线编码" align="center" prop="lineCode" />
    </el-table>
    <pagination
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script>
  import { listQTInfo } from '@/api/function/psInfo';

  export default {
    name: "QtInfo",
    props: {
      orderId: {
        type: Number,
        default: 0,
      },
    },
    watch: {
      orderId: {
        handler(newVal, oldVal) {
          if (newVal) {
            this.queryParams.orderId = newVal;
            this.defaultOrderId = newVal;
            this.getList();
          }
        },
        immediate: true,
        deep: true,
      },

    },
    created() { },
    data() {
      return {
        // 标签打印页面显示
        printFlag: false,
        // 非多个禁用
        multiple: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 子表选中数据
        checkedMDoboxInfoDetail: [],
        printForm: {},

        // 总条数
        total: 0,
        // 字典表格数据
        dataList: [],
        // 默认id
        defaultOrderId: 0,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
      };
    },
    created() {},
    mounted() {},
    methods: {
      /** 查询配送详情列表 */
      getList() {
        this.loading = true;
        listQTInfo(this.queryParams).then((response) => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.queryParams.orderId = this.defaultOrderId;
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.orderId);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
        this.checkedMDoboxInfoDetail = selection;
      },
      handleBatchPrintCode(){
        //必须要等到页面加载完成，不然会报错
        this.$nextTick(()=>{
          setTimeout(()=>{

            // this.$print(this.$refs.print);

            // 获取打印内容的 DOM 元素
            const printContent = this.$refs.print;
            // 打印内容
            const printWindow = window.open('', '_blank');
            printWindow.document.write('<html><head><title>打印</title></head><body>');
            printWindow.document.write(printContent.innerHTML);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            printWindow.print();

          },200)
        })
      },

    },
  };
</script>
